<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-row>
            <el-button type="primary" size="small" @click="onAdd">新增角色</el-button>
          </el-row>
          <el-table
            :data="list"
            border
            stripe
          >
            <el-table-column
              label="序号"
              type="index"
              width="80"
            ></el-table-column>
            <el-table-column
              label="角色"
              prop="name"
            ></el-table-column>
            <el-table-column
              label="描述"
              prop="description"
            ></el-table-column>
            <el-table-column label="操作">
              <template v-slot="{row}">
                <el-button type="success" size="small" @click="onShowAsignPermDialog(row.id)">分配权限</el-button>
                <el-button type="primary" size="small" @click="onUpdate(row.id)">编辑</el-button>
                <el-button type="danger" size="small" @click="onDel(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row type="flex" justify="end" style="margin-top:15px;">
            <el-pagination
              layout="prev, pager, next"
              :page-size="page.pagesize"
              :total="page.total"
              @current-change="onCurrentChange"
            ></el-pagination>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
          >
          </el-alert>
          <!-- 可以通过以下代码快速生成
              el-form>(el-form-item[label]>el-input[v-model="form.xxx" :disabled="true"]{})*4
           -->
          <el-form
            label-width="80px"
            style="width:400px;margin-top:15px"
          >
            <el-form-item label="公司名称">
              <el-input v-model="form.name" disabled></el-input>
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="form.companyAddress" disabled></el-input>
            </el-form-item>
            <el-form-item label="公司邮箱">
              <el-input v-model="form.mailbox" disabled></el-input>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="form.remarks" disabled type="textarea"></el-input>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 弹窗 -->
    <el-dialog
      :visible="showDialog"
      v-bind="$attrs"
      :title="formData.id ? '编辑角色':'新建角色'"
      v-on="$listeners"
      @open="onOpen"
      @close="close"
    >
      <el-form ref="formRef" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入角色名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formData.description" placeholder="请输入角色描述" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>

    <el-dialog
      title="分配权限"
      :visible.sync="showAssignPermDialog"
      width="50%"
    >
      <el-tree ref="treeRef" :data="permList" :props="{label:'name'}" show-checkbox default-expand-all node-key="id" check-strictly></el-tree>
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="showAssignPermDialog = false">取消</el-button>
        <el-button type="primary" size="small" @click="assignPerm">确定</el-button>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import { getCompanyIdAPI, getRolesAPI, addRoleAPI, deleteRoleAPI, getRoleDetailAPI, updateRoleAPI } from '@/api/index.js'
// import { transform } from 'jest.config'
import { translateListToTree } from '@/utils'
import { getPermissionList } from '@/api/permission'
import { getRoleDetail, assignPerm } from '@/api/setting.js'
export default {
  name: 'SettingPage',
  components: {},
  props: {},
  data() {
    return {
      showDialog: false,
      showAssignPermDialog: false,
      permList: [], // 定义数据存储权限的数据
      form: {}, // 定义公司的数据
      list: [], // 定义员工列表的数据
      page: { // 分页信息
        page: 1,
        pagesize: 10,
        total: 0
      },
      formData: {
        name: undefined,
        description: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入角色名称',
          trigger: 'blur'
        }],
        description: []
      },
      // showDialog: false
      currentId: undefined // 当前操作的角色id
    }
  },
  async created() {
    this.form = await getCompanyIdAPI(this.$store.state.user.userInfo.companyId)
    console.log('form', this.form)
    this.getRoles()
  },
  mounted() {},
  methods: {
    async onShowAsignPermDialog(id) {
      this.currentId = id
      // 获取权限树
      this.permList = translateListToTree(await getPermissionList(), '0')
      this.showAssignPermDialog = true
      // 得到选中的权限数据
      const { permIds } = await getRoleDetail(id)
      // await this.$nexTick
      this.$refs.treeRef.setCheckedKeys(permIds)
    },
    async assignPerm() {
      // 获取选中的id,通过组件方法
      const permIds = this.$refs.treeRef.getCheckedKeys() // 确定的时候获取选中的值
      console.log(permIds)
      await assignPerm({
        id: this.currentId,
        permIds
      })
      this.$message.success('操作成功')
      this.showAssignPermDialog = false
    },
    onCurrentChange(val) {
      // val点击的页面值
      this.page.page = val
      // 重新发请求
      this.getRoles()
    },
    async getRoles() {
      const { rows, total } = await getRolesAPI(this.page)
      this.list = rows
      this.page.total = total
    },
    onOpen() {},
    close() {
      this.$refs['formRef'].resetFields()
      this.formData = {
        name: undefined,
        description: undefined
      }
      this.showDialog = false
    },
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        if (this.formData.id) {
          // 编辑 更新
          await updateRoleAPI(this.formData)
        } else {
          // 发请求 新增
          await addRoleAPI(this.formData)
        }
        // 通知成功
        this.$message.success('操作成功')
        // 刷新列表
        this.getRoles()
        this.close()
      })
    },
    // 点击新建角色 新增
    onAdd() {
      this.showDialog = true
    },
    // 更新
    async onUpdate(id) {
      // 回显数据
      this.formData = await getRoleDetailAPI(id)
      this.showDialog = true
    },
    // 删除
    onDel(id) {
      // 确认删除
      // 通过代码片段,elmecon
      // 修改代码片段,C:\Users\当前用户\.vscode\extensions\ss.element-ui-snippets-0.7.2\snippets,搜索elmecon
      // 输入 elmecon 快捷键即可
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        // 发请求
        await deleteRoleAPI(id)
        // 提示成功
        this.$message.success('操作成功')
        // 刷新列表
        this.getRoles()
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>

